package vista;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

import main.Main;
import microprocessador.ULA;
import parser.DicionarioInstrucao;
import parser.Parser;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import excecao.ExcecaoFimPrograma;
import excecao.ExcecaoGeradorEndereco;
import excecao.ExcecaoMemoria;
import excecao.ExcecaoMemoriaControle;
import excecao.ExcecaoParser;
import excecao.ExcecaoRegistrador;
import excecao.ExcecaoULA;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Janela extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	private static final long serialVersionUID = 1L;
	private JSplitPane jSplitPane1;
	private JSplitPane jSplitPane2;
	private JSplitPane jSplitPane4;
	private JCheckBox ckboxFlagP;
	private JCheckBox ckboxFlagN;
	private JCheckBox ckboxFlagZ;
	private JCheckBox ckboxFlagC;
	private JRadioButton rdbtnUla11;
	private JRadioButton rdbtnUla10;
	private JRadioButton rdbtnUla01;
	private JRadioButton rdbtnUla00;
	private JRadioButton rdbtnMuxB1;
	private JRadioButton rdbtnSP;
	private JScrollPane jScrollPane3;
	private JRadioButton rdbtnMuxA2;
	private JLabel lblMicroInstrucao;
	private JLabel lblInstrucao;
	private JList listRegistradores;
	private JList listMemoriaPrincipal;
	private JScrollPane jScrollPane2;
	private JScrollPane jScrollPane1;
	private JLabel jLabel6;
	private JPanel jPanel5;
	private JPanel jPanel4;
	private JCheckBox ckboxFlagO;
	private JRadioButton rdbtnMuxB0;
	private JRadioButton rdbtnMuxA1;
	private JRadioButton rdbtnMuxA0;
	private JRadioButton rdbtnRI;
	private JRadioButton rdbtnRE;
	private JRadioButton rdbtnMP;
	private JRadioButton rdbtnRD;
	private JRadioButton rdbtnMux;
	private JRadioButton rdbtnR4;
	private JRadioButton rdbtnR3;
	private JRadioButton rdbtnR2;
	private JRadioButton rdbtnR1;
	private JRadioButton rdbtnPC;
	private JRadioButton rdbtnA;
	private JRadioButton rdbtnR0;
	private ImagePanel panelImagem;
	private JLabel jLabel4;
	private JPanel jPanel3;
	private JLabel jLabel5;
	private JPanel jPanel2;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JPanel jPanel1;
	private JSplitPane jSplitPane3;
	private JTextArea txtAreaAlgoritmo;
	private JButton btnEncerra;
	private JButton btnPrograma;
	private JButton btnInstrucao;
	private JButton btnMicroInstrucao;
	private JButton btnInicia;
	private JPanel PanelBotoes;
	private JLabel jLabel1;
	private JPanel PanelEsq;	

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void run() {
		Janela inst = new Janela();
		inst.setLocationRelativeTo(null);
		inst.setVisible(true);
	}
	
	public Janela() {
		super();
		createMenu();
		initGUI();
	}
	
	private void createMenu(){
		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);
		
		JMenu menuArquivo = new JMenu("Arquivo");
		menuArquivo.setMnemonic(KeyEvent.VK_A);
		JMenuItem menuItemNovo = new JMenuItem("Novo");
		menuItemNovo.addActionListener(new ListenerMenuNovo());
		JMenuItem menuItemCarregar = new JMenuItem("Carregar");
		menuItemCarregar.addActionListener(new ListenerMenuCarregar());
		JMenuItem menuItemSalvar = new JMenuItem("Salvar");
		menuItemSalvar.addActionListener(new ListenerMenuSalvar());
		JMenuItem menuItemSair = new JMenuItem("Sair");
		menuItemSair.addActionListener(new ListenerMenuSair());
		
		menuArquivo.add(menuItemNovo);
		menuArquivo.add(menuItemCarregar);
		menuArquivo.add(menuItemSalvar);
		menuArquivo.addSeparator();
		menuArquivo.add(menuItemSair);
		
		menuBar.add(menuArquivo);
		
		JMenu menuModoExecucao = new JMenu("Modo de Execução");
		menuModoExecucao.setMnemonic(KeyEvent.VK_M);
		ButtonGroup group = new ButtonGroup();
		JRadioButtonMenuItem menuItemMicro = new JRadioButtonMenuItem("Microinstrução");
		menuItemMicro.addActionListener(new ListenerMicroinstrucao());
		JRadioButtonMenuItem menuItemInstrucao = new JRadioButtonMenuItem("Instrução");
		menuItemInstrucao.addActionListener(new ListenerInstrucao());
		JRadioButtonMenuItem menuItemPrograma = new JRadioButtonMenuItem("Programa", true);
		menuItemPrograma.addActionListener(new ListenerPrograma());
		
		menuModoExecucao.add(menuItemMicro);
		group.add(menuItemMicro);
		menuModoExecucao.add(menuItemInstrucao);
		group.add(menuItemInstrucao);		
		menuModoExecucao.add(menuItemPrograma);
		group.add(menuItemPrograma);
		
		menuBar.add(menuModoExecucao);
		
/*		JMenu menuAjuda = new JMenu("Ajuda");
		menuAjuda.setMnemonic(KeyEvent.VK_J);
		//JMenuItem menuItemConteudo = new JMenuItem("Conteudo");
		JMenuItem menuItemSobre = new JMenuItem("Sobre");
		
		//menuAjuda.add(menuItemConteudo);
		//menuAjuda.addSeparator();
		menuAjuda.add(menuItemSobre);
		
		menuBar.add(menuAjuda);	*/		
	}	
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			this.setTitle("Trabalho de Arquitetura 2010-1");
			{
				jSplitPane1 = new JSplitPane();
				getContentPane().add(jSplitPane1, BorderLayout.CENTER);
				{
					PanelEsq = new JPanel();
					jSplitPane1.add(PanelEsq, JSplitPane.LEFT);
					GroupLayout PanelEsqLayout = new GroupLayout((JComponent)PanelEsq);
					PanelEsq.setLayout(PanelEsqLayout);
					PanelEsq.setPreferredSize(new java.awt.Dimension(259, 547));
					{
						jLabel1 = new JLabel();
						jLabel1.setText("Código");
						jLabel1.setFont(new java.awt.Font("Bitstream Charter",1,16));
					}
					{
						PanelBotoes = new JPanel();
						{
							btnInicia = new JButton();
							PanelBotoes.add(btnInicia);
							btnInicia.setText("Inicia");
							btnInicia.setMnemonic(KeyEvent.VK_I);
							btnInicia.setPreferredSize(new java.awt.Dimension(70, 27));
							btnInicia.addActionListener(new ListenerBotaoIniciar());							
						}
						{
							btnMicroInstrucao = new JButton();
							PanelBotoes.add(btnMicroInstrucao);
							btnMicroInstrucao.setText("Executa");
							btnMicroInstrucao.setMnemonic(KeyEvent.VK_E);
							btnMicroInstrucao.setVisible(false);
							btnMicroInstrucao.addActionListener(new ListenerBotaoExecutaMicroinstrucao());							
						}
						{
							btnInstrucao = new JButton();
							PanelBotoes.add(btnInstrucao);
							btnInstrucao.setText("Executa");
							btnInstrucao.setMnemonic(KeyEvent.VK_E);
							btnInstrucao.setVisible(false);
							this.btnInstrucao.addActionListener(new ListenerBotaoExecutaInstrucao());							
						}
						{
							btnPrograma = new JButton();
							PanelBotoes.add(btnPrograma);
							btnPrograma.setText("Executa");
							btnPrograma.setMnemonic(KeyEvent.VK_E);
							btnPrograma.setVisible(false);
							this.btnPrograma.addActionListener(new ListenerBotaoExecutaPrograma());							
						}
						{
							btnEncerra = new JButton();
							PanelBotoes.add(btnEncerra);
							btnEncerra.setText("Encerra");
							btnEncerra.setMnemonic(KeyEvent.VK_R);
							btnEncerra.setVisible(false);
							this.btnEncerra.addActionListener(new ListenerBotaoEncerra());							
						}
					}
					{
						jScrollPane3 = new JScrollPane();
						{
							txtAreaAlgoritmo = new JTextArea();
							jScrollPane3.setViewportView(txtAreaAlgoritmo);
							txtAreaAlgoritmo.setBorder(BorderFactory.createTitledBorder(""));
						}
					}
					PanelEsqLayout.setHorizontalGroup(PanelEsqLayout.createSequentialGroup()
						.addContainerGap()
						.addGroup(PanelEsqLayout.createParallelGroup()
						    .addGroup(PanelEsqLayout.createSequentialGroup()
						        .addComponent(PanelBotoes, GroupLayout.PREFERRED_SIZE, 247, GroupLayout.PREFERRED_SIZE)
						        .addGap(0, 0, Short.MAX_VALUE))
						    .addGroup(GroupLayout.Alignment.LEADING, PanelEsqLayout.createSequentialGroup()
						        .addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 203, GroupLayout.PREFERRED_SIZE)
						        .addGap(0, 44, Short.MAX_VALUE))
						    .addComponent(jScrollPane3, GroupLayout.Alignment.LEADING, 0, 247, Short.MAX_VALUE))
						.addContainerGap());
					PanelEsqLayout.setVerticalGroup(PanelEsqLayout.createSequentialGroup()
						.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jScrollPane3, 0, 487, Short.MAX_VALUE)
						.addComponent(PanelBotoes, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE));
				}
				{
					jSplitPane2 = new JSplitPane();
					jSplitPane1.add(jSplitPane2, JSplitPane.RIGHT);
					jSplitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
					jSplitPane2.setPreferredSize(new java.awt.Dimension(490,509));
					jSplitPane2.setBackground(new java.awt.Color(255,255,255));
					{
						jSplitPane3 = new JSplitPane();
						jSplitPane2.add(jSplitPane3, JSplitPane.RIGHT);
						jSplitPane3.setOrientation(JSplitPane.VERTICAL_SPLIT);
						jSplitPane3.setSize(490, 270);
						jSplitPane3.setPreferredSize(new java.awt.Dimension(494,230));
						{
							jPanel1 = new JPanel();
							FormLayout jPanel1Layout1 = new FormLayout(
									"max(p;5dlu), max(p;5dlu), 4dlu, 103dlu", 
									"max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu)");
							jSplitPane3.add(jPanel1, JSplitPane.RIGHT);
							jPanel1.setLayout(jPanel1Layout1);
							jPanel1.setPreferredSize(new java.awt.Dimension(492,46));
							{
								jLabel2 = new JLabel();
								jPanel1.add(jLabel2, new CellConstraints("2, 3, 1, 1, default, default"));
								jLabel2.setText("Última Microinstrução:");
								jLabel2.setFont(new java.awt.Font("Lucida Bright",1,14));
							}
							{
								jLabel3 = new JLabel();
								jPanel1.add(jLabel3, new CellConstraints("2, 1, 1, 1, default, default"));
								jLabel3.setText("Última Instrução:");
								jLabel3.setFont(new java.awt.Font("Bitstream Charter",1,14));
							}
							{
								lblInstrucao = new JLabel();
								jPanel1.add(lblInstrucao, new CellConstraints("4, 1, 1, 1, default, default"));
							}
							{
								lblMicroInstrucao = new JLabel();
								jPanel1.add(lblMicroInstrucao, new CellConstraints("4, 3, 1, 1, default, default"));
							}
						}
						{
							jSplitPane4 = new JSplitPane();
							jSplitPane3.add(jSplitPane4, JSplitPane.LEFT);
							jSplitPane4.setPreferredSize(new java.awt.Dimension(492,182));
							{
								jPanel2 = new JPanel();
								jSplitPane4.add(jPanel2, JSplitPane.RIGHT);
								BorderLayout jPanel2Layout = new BorderLayout();
								jPanel2.setLayout(jPanel2Layout);
								jPanel2.setPreferredSize(new java.awt.Dimension(220,202));
								jPanel2.setSize(243, 202);
								{
									jLabel5 = new JLabel();
									jPanel2.add(jLabel5, BorderLayout.NORTH);
									jLabel5.setText("Registradores");
									jLabel5.setPreferredSize(new java.awt.Dimension(240,24));
									jLabel5.setFont(new java.awt.Font("Lucida Bright",1,14));
								}
								{
									jScrollPane2 = new JScrollPane();
									jPanel2.add(jScrollPane2, BorderLayout.CENTER);
									{
/*										ListModel listRegistradoresModel = 
											new DefaultComboBoxModel(
													new String[] { "", "" });*/
										listRegistradores = new JList();
										jScrollPane2.setViewportView(listRegistradores);
										//listRegistradores.setModel(listRegistradoresModel);
									}
								}
							}
							{
								jPanel3 = new JPanel();
								jSplitPane4.add(jPanel3, JSplitPane.LEFT);
								BorderLayout jPanel3Layout = new BorderLayout();
								jPanel3.setLayout(jPanel3Layout);
								jPanel3.setPreferredSize(new java.awt.Dimension(237,202));
								jPanel3.setSize(243, 202);
								jPanel3.setEnabled(false);
								{
									jLabel4 = new JLabel();
									jPanel3.add(jLabel4, BorderLayout.NORTH);
									jLabel4.setText("Memória");
									jLabel4.setPreferredSize(new java.awt.Dimension(237,23));
									jLabel4.setFont(new java.awt.Font("Lucida Bright",1,14));
								}
								{
									jScrollPane1 = new JScrollPane();
									jPanel3.add(jScrollPane1, BorderLayout.CENTER);
									jScrollPane1.setPreferredSize(new java.awt.Dimension(237, 170));
									{
/*										ListModel listMemoriaPrincipalModel = 
											new DefaultComboBoxModel(
													new String[] { "", "" });*/
										listMemoriaPrincipal = new JList();
										jScrollPane1.setViewportView(listMemoriaPrincipal);
										//listMemoriaPrincipal.setModel(listMemoriaPrincipalModel);
									}
								}
							}
						}
					}
					{
						panelImagem = new ImagePanel("image.png");
						jSplitPane2.add(panelImagem, JSplitPane.TOP);
						panelImagem.setLayout(null);
						panelImagem.setPreferredSize(new java.awt.Dimension(500, 264));
						panelImagem.setBackground(new java.awt.Color(255,255,255));
						{
							rdbtnR0 = new JRadioButton();
							panelImagem.add(rdbtnR0);
							rdbtnR0.setBounds(165, -2, 16, 20);
							rdbtnR0.setFont(new java.awt.Font("Bitstream Charter",1,10));
							rdbtnR0.setBackground(new java.awt.Color(255,255,255));
							rdbtnR0.setEnabled(false);
						}
						{
							rdbtnA = new JRadioButton();
							panelImagem.add(rdbtnA);
							rdbtnA.setBounds(134, -2, 16, 20);
							rdbtnA.setBackground(new java.awt.Color(255,255,255));
							rdbtnA.setEnabled(false);
						}
						{
							rdbtnPC = new JRadioButton();
							panelImagem.add(rdbtnPC);
							rdbtnPC.setBounds(196, -1, 16, 19);
							rdbtnPC.setBackground(new java.awt.Color(255,255,255));
							rdbtnPC.setEnabled(false);
						}
						{
							rdbtnR1 = new JRadioButton();
							panelImagem.add(rdbtnR1);
							rdbtnR1.setBounds(332, 0, 18, 18);
							rdbtnR1.setBackground(new java.awt.Color(255,255,255));
							rdbtnR1.setEnabled(false);
						}
						{
							rdbtnR2 = new JRadioButton();
							panelImagem.add(rdbtnR2);
							rdbtnR2.setBackground(new java.awt.Color(255,255,255));
							rdbtnR2.setBounds(359, 0, 18, 18);
							rdbtnR2.setEnabled(false);
						}
						{
							rdbtnR3 = new JRadioButton();
							panelImagem.add(rdbtnR3);
							rdbtnR3.setBackground(new java.awt.Color(255,255,255));
							rdbtnR3.setBounds(387, 0, 18, 18);
							rdbtnR3.setEnabled(false);
						}
						{
							rdbtnR4 = new JRadioButton();
							panelImagem.add(rdbtnR4);
							rdbtnR4.setBackground(new java.awt.Color(255,255,255));
							rdbtnR4.setBounds(416, 0, 18, 18);
							rdbtnR4.setEnabled(false);
						}
						{
							rdbtnMux = new JRadioButton();
							panelImagem.add(rdbtnMux);
							rdbtnMux.setBounds(81, 17, 22, 19);
							rdbtnMux.setBackground(new java.awt.Color(255,255,255));
							rdbtnMux.setEnabled(false);
						}
						{
							rdbtnRD = new JRadioButton();
							panelImagem.add(rdbtnRD);
							rdbtnRD.setBounds(10, 50, 26, 17);
							rdbtnRD.setBackground(new java.awt.Color(255,255,255));
							rdbtnRD.setEnabled(false);
						}
						{
							rdbtnMP = new JRadioButton();
							panelImagem.add(rdbtnMP);
							rdbtnMP.setBounds(91, 123, 28, 17);
							rdbtnMP.setBackground(new java.awt.Color(255,255,255));
							rdbtnMP.setEnabled(false);
						}
						{
							rdbtnRE = new JRadioButton();
							panelImagem.add(rdbtnRE);
							rdbtnRE.setBounds(10, 215, 24, 18);
							rdbtnRE.setBackground(new java.awt.Color(255,255,255));
							rdbtnRE.setEnabled(false);
						}
						{
							rdbtnRI = new JRadioButton();
							panelImagem.add(rdbtnRI);
							rdbtnRI.setBounds(94, 215, 25, 18);
							rdbtnRI.setBackground(new java.awt.Color(255,255,255));
							rdbtnRI.setEnabled(false);
						}
						{
							rdbtnMuxA0 = new JRadioButton();
							panelImagem.add(rdbtnMuxA0);
							rdbtnMuxA0.setBounds(255, 75, 29, 18);
							rdbtnMuxA0.setBackground(new java.awt.Color(255,255,255));
							rdbtnMuxA0.setEnabled(false);
						}
						{
							rdbtnMuxA1 = new JRadioButton();
							panelImagem.add(rdbtnMuxA1);
							rdbtnMuxA1.setBounds(244, 87, 36, 18);
							rdbtnMuxA1.setBackground(new java.awt.Color(255,255,255));
							rdbtnMuxA1.setEnabled(false);
						}
						{
							rdbtnMuxB0 = new JRadioButton();
							panelImagem.add(rdbtnMuxB0);
							rdbtnMuxB0.setBounds(453, 82, 24, 17);
							rdbtnMuxB0.setBackground(new java.awt.Color(255,255,255));
							rdbtnMuxB0.setEnabled(false);
						}
						{
							rdbtnMuxB1 = new JRadioButton();
							panelImagem.add(rdbtnMuxB1);
							rdbtnMuxB1.setBounds(444, 94, 24, 18);
							rdbtnMuxB1.setBackground(new java.awt.Color(255,255,255));
							rdbtnMuxB1.setEnabled(false);
						}
						{
							rdbtnUla00 = new JRadioButton();
							panelImagem.add(rdbtnUla00);
							rdbtnUla00.setBounds(362, 132, 26, 17);
							rdbtnUla00.setBackground(new java.awt.Color(255,255,255));
							rdbtnUla00.setEnabled(false);
						}
						{
							rdbtnUla01 = new JRadioButton();
							panelImagem.add(rdbtnUla01);
							rdbtnUla01.setBounds(354, 147, 26, 17);
							rdbtnUla01.setBackground(new java.awt.Color(255,255,255));
							rdbtnUla01.setEnabled(false);
						}
						{
							rdbtnUla10 = new JRadioButton();
							panelImagem.add(rdbtnUla10);
							rdbtnUla10.setBounds(362, 162, 26, 17);
							rdbtnUla10.setBackground(new java.awt.Color(255,255,255));
							rdbtnUla10.setEnabled(false);
						}
						{
							rdbtnUla11 = new JRadioButton();
							panelImagem.add(rdbtnUla11);
							rdbtnUla11.setBounds(355, 178, 26, 17);
							rdbtnUla11.setBackground(new java.awt.Color(255,255,255));
							rdbtnUla11.setEnabled(false);
						}
						{
							jPanel5 = new JPanel();
							BorderLayout jPanel5Layout = new BorderLayout();
							jPanel5.setLayout(jPanel5Layout);
							panelImagem.add(jPanel5);
							jPanel5.setBounds(400, 130, 83, 91);
							jPanel5.setBackground(new java.awt.Color(255,255,255));
							{
								jLabel6 = new JLabel();
								jPanel5.add(jLabel6, BorderLayout.NORTH);
								jLabel6.setText("Flags");
								jLabel6.setPreferredSize(new java.awt.Dimension(67, 20));
								jLabel6.setFont(new java.awt.Font("Lucida Bright",1,14));
							}
							{
								jPanel4 = new JPanel();
								jPanel5.add(jPanel4, BorderLayout.CENTER);
								GridLayout jPanel4Layout = new GridLayout(3, 2);
								jPanel4Layout.setHgap(5);
								jPanel4Layout.setVgap(5);
								jPanel4Layout.setColumns(2);
								jPanel4Layout.setRows(3);
								jPanel4.setLayout(jPanel4Layout);
								jPanel4.setBounds(410, 161, 108, 86);
								jPanel4.setBackground(new java.awt.Color(255,255,255));
								jPanel4.setPreferredSize(new java.awt.Dimension(49, 71));
								{
									ckboxFlagZ = new JCheckBox();
									jPanel4.add(ckboxFlagZ);
									ckboxFlagZ.setText("Z");
									ckboxFlagZ.setBounds(411, 193, 41, 18);
									ckboxFlagZ.setBackground(new java.awt.Color(255,255,255));
									ckboxFlagZ.setEnabled(false);
								}
								{
									ckboxFlagN = new JCheckBox();
									jPanel4.add(ckboxFlagN);
									ckboxFlagN.setText("N");
									ckboxFlagN.setBounds(411, 209, 53, 18);
									ckboxFlagN.setBackground(new java.awt.Color(255,255,255));
									ckboxFlagN.setPreferredSize(new java.awt.Dimension(-1, 33));
									ckboxFlagN.setEnabled(false);
								}
								{
									ckboxFlagP = new JCheckBox();
									jPanel4.add(ckboxFlagP);
									ckboxFlagP.setText("P");
									ckboxFlagP.setBounds(411, 224, 53, 18);
									ckboxFlagP.setBackground(new java.awt.Color(255,255,255));
									ckboxFlagP.setEnabled(false);
								}
								{
									ckboxFlagO = new JCheckBox();
									jPanel4.add(ckboxFlagO);
									ckboxFlagO.setText("O");
									ckboxFlagO.setBounds(411, 239, 60, 18);
									ckboxFlagO.setBackground(new java.awt.Color(255,255,255));
									ckboxFlagO.setEnabled(false);
								}
								{
									ckboxFlagC = new JCheckBox();
									jPanel4.add(ckboxFlagC);
									ckboxFlagC.setText("C");
									ckboxFlagC.setBounds(411, 175, 60, 18);
									ckboxFlagC.setBackground(new java.awt.Color(255,255,255));
									ckboxFlagC.setEnabled(false);
								}
							}
						}
						{
							rdbtnSP = new JRadioButton();
							panelImagem.add(rdbtnSP);
							rdbtnSP.setBounds(224, -1, 18, 18);
							rdbtnSP.setEnabled(false);
						}
						{
							rdbtnMuxA2 = new JRadioButton();
							panelImagem.add(rdbtnMuxA2);
							rdbtnMuxA2.setBounds(234, 99, 18, 18);
							rdbtnMuxA2.setEnabled(false);
						}
					}
				}
			}
			preencheCampos();
			pack();
			this.setSize(800, 600);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	
	public void preencheCampos(){
		preencheMemoria();
		preencheRegistradores();
		preencheMicroInstrucao();
		preencheInstrucao();
		preencheFlags();
		preenchePontosControle();
	}
	
	private void preencheMemoria(){
		int tam = Main.getMemoriaPrincipal().getTamanho();
		String[] str = new String[tam];
		int i = 0;
		for (String s : Main.getMemoriaPrincipal().getDado()){
			str[i] = String.valueOf(i) + ": " + s;
			i++;
		}
		
		this.listMemoriaPrincipal.setListData(str);		
	}
	
	private void preencheRegistradores(){
		String[] str = new String[11];
		str[0] = Main.getProcessador().getR()[0].getNome() + ": " + Main.getProcessador().getR()[0].getRegistro();
		str[1] = Main.getProcessador().getR()[1].getNome() + ": " + Main.getProcessador().getR()[1].getRegistro();
		str[2] = Main.getProcessador().getR()[2].getNome() + ": " + Main.getProcessador().getR()[2].getRegistro();
		str[3] = Main.getProcessador().getR()[3].getNome() + ": " + Main.getProcessador().getR()[3].getRegistro();
		str[4] = Main.getProcessador().getR()[4].getNome() + ": " + Main.getProcessador().getR()[4].getRegistro();
		
		str[5] = Main.getProcessador().getA().getNome() + ": " + Main.getProcessador().getA().getRegistro();
		str[6] = Main.getProcessador().getPC().getNome() + ": " + Main.getProcessador().getPC().getRegistro();
		str[7] = Main.getProcessador().getSP().getNome() + ": " + Main.getProcessador().getSP().getRegistro();
		str[8] = Main.getProcessador().getRegEndereco().getNome() + ": " + Main.getProcessador().getRegEndereco().getRegistro();
		str[9] = Main.getProcessador().getRegDados().getNome() + ": " + Main.getProcessador().getRegDados().getRegistro();
		str[10] = Main.getProcessador().getRegInstrucao().getNome() + ": " + Main.getProcessador().getRegInstrucao().getRegistro();
					
		this.listRegistradores.setListData(str);		
	}
	
	private void preencheMicroInstrucao(){
		String microinstrucao = Main.getProcessador().getUnidadeControle().getNomeMicroinstrucao();
		
		lblMicroInstrucao.setText(microinstrucao);		
	}
	
	private void preencheInstrucao(){
		String codInstrucao = Main.getProcessador().getRegInstrucao().getRegistro();
		String instrucao = DicionarioInstrucao.getNomeInstrucao(codInstrucao);
		
		lblInstrucao.setText(instrucao);		
	}
	
	private void preencheFlags(){
		String flags = Main.getProcessador().getMyULA().getFlags().getRegistro();
		ckboxFlagC.setSelected(flags.charAt(ULA.C) == '1');
		ckboxFlagZ.setSelected(flags.charAt(ULA.Z) == '1');
		ckboxFlagN.setSelected(flags.charAt(ULA.N) == '1');
		ckboxFlagP.setSelected(flags.charAt(ULA.P) == '1');
		ckboxFlagO.setSelected(flags.charAt(ULA.O) == '1');
	}
	
	private void preenchePontosControle(){
		rdbtnR0.setSelected(Main.getProcessador().getR()[0].getControle() == '1');
		rdbtnR1.setSelected(Main.getProcessador().getR()[1].getControle() == '1');
		rdbtnR2.setSelected(Main.getProcessador().getR()[2].getControle() == '1');
		rdbtnR3.setSelected(Main.getProcessador().getR()[3].getControle() == '1');
		rdbtnR4.setSelected(Main.getProcessador().getR()[4].getControle() == '1');
		
		rdbtnA.setSelected(Main.getProcessador().getA().getControle() == '1');
		rdbtnPC.setSelected(Main.getProcessador().getPC().getControle() == '1');
		rdbtnSP.setSelected(Main.getProcessador().getSP().getControle() == '1');
		rdbtnRE.setSelected(Main.getProcessador().getRegEndereco().getControle() == '1');
		rdbtnRD.setSelected(Main.getProcessador().getRegDados().getControle() == '1');
		rdbtnRI.setSelected(Main.getProcessador().getRegInstrucao().getControle() == '1');
		
		rdbtnUla00.setSelected(Main.getProcessador().getMyULA().getCodOperacao().charAt(0) == '1');
		rdbtnUla01.setSelected(Main.getProcessador().getMyULA().getCodOperacao().charAt(1) == '1');
		rdbtnUla10.setSelected(Main.getProcessador().getMyULA().getCodOperacao().charAt(2) == '1');
		rdbtnUla11.setSelected(Main.getProcessador().getMyULA().getCodOperacao().charAt(3) == '1');		
		
		rdbtnMux.setSelected(Main.getMemoriaPrincipal().getControle() == '1');
		
		rdbtnMuxA0.setSelected(Main.getProcessador().getMuxA().getSeletor().charAt(0) == '1');
		rdbtnMuxA1.setSelected(Main.getProcessador().getMuxA().getSeletor().charAt(1) == '1');
		rdbtnMuxA2.setSelected(Main.getProcessador().getMuxA().getSeletor().charAt(2) == '1');
		
		rdbtnMuxB0.setSelected(Main.getProcessador().getMuxB().getSeletor().charAt(0) == '1');
		rdbtnMuxB1.setSelected(Main.getProcessador().getMuxB().getSeletor().charAt(1) == '1');
		
		rdbtnMux.setSelected(Main.getProcessador().getMux().getSeletor().charAt(0) == '1');
	}	

	class ListenerBotaoIniciar implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int pc = 10;
			
			String texto = Janela.this.txtAreaAlgoritmo.getText();
			try {
				Parser.getInstancia().parserTexto(texto);
				Parser.getInstancia().insereMemoria(pc, Main.getMemoriaPrincipal());
				
				Janela.this.txtAreaAlgoritmo.setEditable(false);
				
				Janela.this.btnInicia.setEnabled(false);
				Janela.this.btnInicia.setVisible(false);
				
				Janela.this.btnEncerra.setEnabled(true);
				Janela.this.btnEncerra.setVisible(true);						
				
				switch (Main.getModoExecucao()){
				case Main.modoInstrucao:
					Janela.this.btnInstrucao.setEnabled(true);
					Janela.this.btnInstrucao.setVisible(true);				
					break;
				case Main.modoMicroinstrucao:
					Janela.this.btnMicroInstrucao.setEnabled(true);
					Janela.this.btnMicroInstrucao.setVisible(true);				
					break;
				case Main.modoPrograma:
					Janela.this.btnPrograma.setEnabled(true);
					Janela.this.btnPrograma.setVisible(true);				
					break;				
				}
				
				
				String pos_bios = Integer.toBinaryString(pc);
				String pc_bios = ULA.geraComplemento(pos_bios.length()) + pos_bios;
				pos_bios = Integer.toBinaryString(Main.getMemoriaPrincipal().getTamanho()-1);
				String sp_bios = ULA.geraComplemento(pos_bios.length()) + pos_bios;
				Main.getProcessador().simulaBIOS(pc_bios, sp_bios);
				
				Janela.this.preencheCampos();				
			} catch (ExcecaoMemoria e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoParser e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoRegistrador e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			}
		}
	}
	
	class ListenerBotaoExecutaMicroinstrucao implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				Main.getProcessador().executaMicro();
				Janela.this.preencheCampos();
			} catch (ExcecaoULA e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoMemoria e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoGeradorEndereco e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoMemoriaControle e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoFimPrograma e1) {
				// TODO Auto-generated catch block
				new FimProgramaDialog(null, "Fim do Programa");
			} catch (ExcecaoRegistrador e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			}
		}
	}
	
	class ListenerBotaoExecutaInstrucao implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				Main.getProcessador().executaInstrucao();
				Janela.this.preencheCampos();
			} catch (ExcecaoULA e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoMemoria e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoGeradorEndereco e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoMemoriaControle e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoFimPrograma e1) {
				// TODO Auto-generated catch block
				new FimProgramaDialog(null, "Fim do Programa");
			} catch (ExcecaoRegistrador e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			}
		}
	}	
	
	class ListenerBotaoExecutaPrograma implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				Main.getProcessador().executaPrograma();
			} catch (ExcecaoULA e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoMemoria e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoGeradorEndereco e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoMemoriaControle e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			} catch (ExcecaoFimPrograma e1) {
				// TODO Auto-generated catch block
				new FimProgramaDialog(null, "Fim do Programa");
			} catch (ExcecaoRegistrador e1) {
				// TODO Auto-generated catch block
				new ErroDialog(null, e1.getMensagem(), e1.getTipo());
			}
			
			Janela.this.preencheCampos();
		}
	}	
	
	class ListenerBotaoEncerra implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {			
			Janela.this.txtAreaAlgoritmo.setEditable(true);
			
			Janela.this.lblMicroInstrucao.setText("");
			Janela.this.lblInstrucao.setText("");

			Janela.this.btnEncerra.setEnabled(false);
			Janela.this.btnEncerra.setVisible(false);			
			
			Janela.this.btnInicia.setEnabled(true);
			Janela.this.btnInicia.setVisible(true);

			Janela.this.btnInstrucao.setEnabled(false);
			Janela.this.btnInstrucao.setVisible(false);				

			Janela.this.btnMicroInstrucao.setEnabled(false);
			Janela.this.btnMicroInstrucao.setVisible(false);
			
			Janela.this.btnPrograma.setEnabled(false);
			Janela.this.btnPrograma.setVisible(false);				
		}
	}	
	
	class ListenerMenuNovo implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {			
			Janela.this.txtAreaAlgoritmo.setText("");
		}
	}
	
	class ListenerMenuCarregar implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			JFileChooser fileChooser = new JFileChooser();
			
			int returnVal = fileChooser.showOpenDialog(new JDialog());
	
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fileChooser.getSelectedFile();
	            this.carregar(file);
	        }
		}
		
		private void carregar(File file){
			String texto = readFile(file);
			Janela.this.txtAreaAlgoritmo.setText(texto);
		}
		
		private String readFile(File file){	
			String str = "";
			
			try{    
				String line;
	            BufferedReader in = new BufferedReader(new FileReader(file));

	            if (!in.ready())
	                throw new IOException();

	            while ((line = in.readLine()) != null)
	                str = str + line + "\n";

	            in.close();
	        }
	        catch (IOException e){
	            System.out.println(e);
	        }
	        
	        return str;
		}
	}
	
	class ListenerMenuSalvar implements ActionListener{
	
		@Override
		public void actionPerformed(ActionEvent e) {
			JFileChooser fileChooser = new JFileChooser();
			//fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	
			int returnVal = fileChooser.showSaveDialog(new JDialog());
	
	        if (returnVal == JFileChooser.APPROVE_OPTION) {
	            File file = fileChooser.getSelectedFile();
	            this.salvar(file);
	        }
		}
		
		
		private void salvar(File file){
			String texto = Janela.this.txtAreaAlgoritmo.getText();
			this.saveFile(file, texto);
		}
		
		private void saveFile(File file, String text){	
			
	        BufferedWriter out = null;
	        
	        try {
	        	out = new BufferedWriter(new FileWriter(file));
	        	out.write(text);
	        }
	        catch (IOException ex) {
	            ex.printStackTrace();
	        }
	        finally {
	            try {
	                if (out != null) {
	                	out.flush();
	                	out.close();
	                }
	            }
	            catch (IOException ex) {
	                ex.printStackTrace();
	            }
	        }
	    }	
	}
	
	class ListenerMenuSair implements ActionListener{
	
		@Override
		public void actionPerformed(ActionEvent e) {
			Janela.this.dispose();
		}
	}
	
	class ListenerMicroinstrucao implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			Main.setModoExecucao(Main.modoMicroinstrucao);
			Janela.this.jLabel2.setText("Última Microinstrução:");
			Janela.this.jLabel3.setText("Última Instrução:");
			
			if (Janela.this.btnInicia.isVisible()){
				Janela.this.btnMicroInstrucao.setVisible(false);
				Janela.this.btnMicroInstrucao.setEnabled(false);
			}
			else{
				Janela.this.btnMicroInstrucao.setVisible(true);
				Janela.this.btnMicroInstrucao.setEnabled(true);
			}
			Janela.this.btnInstrucao.setVisible(false);
			Janela.this.btnInstrucao.setEnabled(false);
			
			Janela.this.btnPrograma.setVisible(false);
			Janela.this.btnPrograma.setEnabled(false);
		}
	}
	
	class ListenerInstrucao implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			Main.setModoExecucao(Main.modoInstrucao);
			Janela.this.jLabel2.setText("Prómixa Microinstrução:");
			Janela.this.jLabel3.setText("Próxima Instrução:");			
			
			Janela.this.btnMicroInstrucao.setVisible(false);
			Janela.this.btnMicroInstrucao.setEnabled(false);
			
			if (Janela.this.btnInicia.isVisible()){
				Janela.this.btnInstrucao.setVisible(false);
				Janela.this.btnInstrucao.setEnabled(false);
			}
			else{
				Janela.this.btnInstrucao.setVisible(true);
				Janela.this.btnInstrucao.setEnabled(true);
			}
			
			Janela.this.btnPrograma.setVisible(false);
			Janela.this.btnPrograma.setEnabled(false);
		}
	}
	
	class ListenerPrograma implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			Janela.this.jLabel2.setText("Última Microinstrução:");
			Janela.this.jLabel3.setText("Última Instrução:");
			
			Main.setModoExecucao(Main.modoPrograma);
			
			Janela.this.btnMicroInstrucao.setVisible(false);
			Janela.this.btnMicroInstrucao.setEnabled(false);
			
			Janela.this.btnInstrucao.setVisible(false);
			Janela.this.btnInstrucao.setEnabled(false);
			
			if (Janela.this.btnInicia.isVisible()){
				Janela.this.btnPrograma.setVisible(false);
				Janela.this.btnPrograma.setEnabled(false);
			}
			else{
				Janela.this.btnPrograma.setVisible(true);
				Janela.this.btnPrograma.setEnabled(true);
			}
		}
	}
}